package inet.app.vod.subtitles;

import inet.app.vod.video.MyVideoView;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map.Entry;

import android.util.Log;

public class SubtitleController {
	
	private enum TYPE_SUBTITLES {
		ASS,
		SCC,
		SRT,
		STL,
		TTML,
		NOT_SUPPORT
	}
	
	private final static String TAG = "subtitle_controller";
	
	private MyVideoView mVideoView;
	private TimedTextObject mTTO;
	private SubtitleListener mSubtitleListener;
	private SynchronizeSubtitle mSynchSubtitle;
	
	public SubtitleController(MyVideoView videoView) {
		this.mVideoView = videoView;
	}
	
	/**
	 * Load subtitle from file on sdcard
	 * @param subtitlePath
	 * 		path of file subtitle
	 * @return true if load success
	 * 		   false if load fail
	 */
	public boolean loadSubtitles(String subtitlePath) {
		
		TimedTextFileFormat ttff = null;
		
		TYPE_SUBTITLES extType = getExtSubtitles(subtitlePath);
		if(TYPE_SUBTITLES.NOT_SUPPORT == extType) {
			Log.e(TAG, "type file not support");
			return false;
		}
		else if(TYPE_SUBTITLES.ASS == extType) {
			ttff = new FormatASS();
		}
		else if(TYPE_SUBTITLES.SCC == extType) {
			ttff = new FormatSCC();
		}
		else if(TYPE_SUBTITLES.SRT == extType) {
			ttff = new FormatSRT();
		}
		else if(TYPE_SUBTITLES.STL == extType) {
			ttff = new FormatSTL();
		}
		else if(TYPE_SUBTITLES.TTML == extType) {
			ttff = new FormatTTML();
		}
		
		try {
			File file = new File(subtitlePath);
			InputStream is = new FileInputStream(file);
			mTTO = ttff.parseFile(subtitlePath, is);
			for(Entry<Integer, Caption> entry: mTTO.captions.entrySet()) {
				Log.d(TAG, entry.getValue().content);
			}
			return true;
		} catch (FileNotFoundException e) {
			Log.e(TAG, "file subtitle isn't exist");
			return false;
		} catch (IOException e) {
			Log.e(TAG, e.toString());
			return false;
		} catch (FatalParsingException e) {
			Log.e(TAG, e.toString());
			return false;
		}
	}
	
	/**
	 * start synchronize subtutile 
	 */
	public void startSynchSubtitle() {
		
		if(null == mSynchSubtitle) {
			Log.d(TAG, "start synchronize subtitle");
	
			mSynchSubtitle = new SynchronizeSubtitle(mVideoView, mTTO, mSubtitleListener);
			mSynchSubtitle.start();
		}
	}
	
	public void resumeSynchSubtitle() {
		if(null != mSynchSubtitle) {
			mSynchSubtitle.onResume();
		}
	}
	
	public void pauseSynchSubtitle() {
		if(null != mSynchSubtitle) {
			mSynchSubtitle.onPause();
		}
	}
	
	public void stopSynchSubtitle() {
		if(null != mSynchSubtitle) {
			mSynchSubtitle.kill();
		}
	}
	
	/**
	 * Get extension of file subtitle
	 */
	private TYPE_SUBTITLES getExtSubtitles(String filePath) {
		
		if(filePath.endsWith(".ass")) {
			return TYPE_SUBTITLES.ASS;
		}
		else if(filePath.endsWith(".scc")) {
			return TYPE_SUBTITLES.SCC;
		}
		else if(filePath.endsWith(".srt")) {
			return TYPE_SUBTITLES.SRT;
		}
		else if(filePath.endsWith(".stl")) {
			return TYPE_SUBTITLES.STL;
		}
		else if(filePath.endsWith(".ttml")) {
			return TYPE_SUBTITLES.TTML;
		}
		else {
			return TYPE_SUBTITLES.NOT_SUPPORT;
		}
	}
	
	public void setSubtitleListener(SubtitleListener listener) {
		this.mSubtitleListener = listener;
	}
	
	public interface SubtitleListener {
		public void onUpdateSubtitle(String subtitle);
	}
}
